SHELL = @SHELL@
VERSION = @PACKAGE_VERSION@

srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@

bindir = @bindir@
sbindir = @sbindir@
libexecdir = @libexecdir@
datadir = @datadir@
datarootdir = @datarootdir@
sysconfdir = @sysconfdir@
sharedstatedir = @sharedstatedir@
localstatedir = @localstatedir@
libdir = @libdir@
infodir = @infodir@
includedir = @includedir@
licensesdir = $(datadir)/licenses

mandir = @mandir@
man1dir = $(mandir)/man1
man4dir = $(mandir)/man4

CC = @CC@
CFLAGS = @CFLAGS@
DEFS = @DEFS@
LDFLAGS = @LDFLAGS@

AWK = @AWK@
CHGRP = @CHGRP@
CHMOD = @CHMOD@
CHOWN = @CHOWN@
CPIO = @CPIO@
FIND = @FIND@
EGREP = @EGREP@
INSTALL = @INSTALL@
LN = @LN@
TAR = @TAR@
TOUCH = @TOUCH@
PYTHON = @PYTHON@
SED = @SED@
UNZIP = @UNZIP@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS)
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@

mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs

sbutils_MAN1 = \
	sb.1

sbutils_MAN4 = \
	depot-db.xml.4 \
	sb-db.xml.4 \
	sbutils.conf.4

sbutils_lib_SCRIPTS = \
	baseclass/__init__.py \
	baseclass/configbase.py \
	baseclass/depotbase.py \
	classutil.py \
	config.py \
	depot/__init__.py \
	depot/file.py \
	depot/ftp.py \
	depot/http.py \
	depot/https.py \
	depot/sftp.py \
	depotdb.py \
	lockfile.py \
	misclib.py \
	myio.py \
	mysig.py \
	packagenames.py \
	prog/__init__.py \
	prog/archive/__init__.py \
	prog/archive/cpio.py \
	prog/archive/patch.py \
	prog/archive/sh.py \
	prog/archive/tar.py \
	prog/archive/xmlplain.py \
	prog/archive/zip.py \
	progress_meter.py \
	sbdb.py \
	textgroup.py \
	version.py \
	xmlutil.py

licenses_DATA = \
	apache-2.0.license \
	artistic.license \
	gfdl-1.3.license \
	gfdl-1.2.license \
	gpl-2.license \
	gpl-3.license \
	lgpl-2.license \
	lgpl-2.1.license \
	lgpl-3.license

all: gpp/gpp gpp/gpp.1

gpp/gpp: gpp/gpp.c
	$(CC) -o $@ $(CFLAGS) $(DEFS) $(LDFLAGS) $<

gpp/gpp.1: gpp/gpp gpp/gpphelp.pp
	gpp/gpp -H -Dman gpp/gpphelp.pp -o $@

install: gpp/gpp
	$(mkinstalldirs) $(bindir)
	for prog in gen-sb-db sb; do \
	  sed -e "s!%prefix%!$(prefix)!" \
	  -e "s!%libdir%!$(libdir)!" \
	  -e "s!%datadir%!$(datadir)!" \
	  -e "s!%sysconfdir%!$(sysconfdir)!" \
	  -e "s!%PYTHON%!$(PYTHON)!" \
	  -e "s!%VERSION%!$(VERSION)!" \
	  $(srcdir)/bin/$${prog} >$(bindir)/$${prog}; \
	  chmod 755 $(bindir)/$${prog}; \
	done

	# install sample configuration file
	$(mkinstalldirs) $(sysconfdir)
	sed -e "s!%libdir%!$(libdir)!" -e "s!%datadir%!$(datadir)!" \
	$(srcdir)/etc/sbutils.conf.sample >$(sysconfdir)/sbutils.conf.sample
	chmod 644 $(sysconfdir)/sbutils.conf.sample

	# install data files
	$(mkinstalldirs) $(datadir)
	$(INSTALL_DATA) $(srcdir)/share/depot-db.dtd $(datadir)
	$(INSTALL_DATA) $(srcdir)/share/sb-db.dtd $(datadir)

	# install license files
	$(mkinstalldirs) $(licensesdir)
	for license in $(licenses_DATA); do \
	  cp share/licenses/$${license} $(licensesdir); \
	done

	# install library files
	$(mkinstalldirs) $(libdir)
	$(mkinstalldirs) $(libdir)/baseclass
	$(mkinstalldirs) $(libdir)/depot
	$(mkinstalldirs) $(libdir)/prog
	$(mkinstalldirs) $(libdir)/prog/archive
	$(INSTALL_SCRIPT) lib/systype $(libdir)/systype
	(cd lib; for prog in $(sbutils_lib_SCRIPTS); do \
	  sed -e "s!%prefix%!$(prefix)!" \
	  -e "s!%bindir%!$(bindir)!" \
	  -e "s!%libdir%!$(libdir)!" \
	  -e "s!%datadir%!$(datadir)!" \
	  -e "s!%sysconfdir%!$(sysconfdir)!" \
	  -e "s!%AWK%!$(AWK)!" \
	  -e "s!%CHGRP%!$(CHGRP)!" \
	  -e "s!%CHMOD%!$(CHMOD)!" \
	  -e "s!%CHOWN%!$(CHOWN)!" \
	  -e "s!%CPIO%!$(CPIO)!" \
	  -e "s!%FIND%!$(FIND)!" \
	  -e "s!%EGREP%!$(EGREP)!" \
	  -e "s!%INSTALL%!$(INSTALL)!" \
	  -e "s!%LN%!$(LN)!" \
	  -e "s!%TAR%!$(TAR)!" \
	  -e "s!%TOUCH%!$(TOUCH)!" \
	  -e "s!%SED%!$(SED)!" \
	  -e "s!%UNZIP%!$(UNZIP)!" \
	  -e "s!%VERSION%!$(VERSION)!" \
	  $${prog} >$(libdir)/$${prog}; \
	  echo "compiling $(libdir)/$$prog"; \
	  $(PYTHON) -c "import py_compile; \
	  py_compile.compile ('$${prog}', '$(libdir)/$${prog}c')"; \
	  chmod 644 $(libdir)/$${prog}c; \
	done)

	# install auxiliary files
	$(mkinstalldirs) $(libdir)/aux
	$(mkinstalldirs) $(libdir)/aux/bin
	$(mkinstalldirs) $(libdir)/aux/man
	$(mkinstalldirs) $(libdir)/aux/man/man1
	$(INSTALL_SCRIPT) gpp/gpp $(libdir)/aux/bin
	$(INSTALL_DATA) gpp/gpp.1 $(libdir)/aux/man/man1
	$(INSTALL_SCRIPT) config.guess $(libdir)/aux/bin/systype

	# install man pages
	$(mkinstalldirs) $(mandir)/man1
	(for man in $(sbutils_MAN1); do \
	  sed -e "s!%prefix%!$(prefix)!" \
	  -e "s!%libdir%!$(libdir)!" \
	  -e "s!%datadir%!$(datadir)!" \
	  -e "s!%sysconfdir%!$(sysconfdir)!" \
	  -e "s!%VERSION%!$(VERSION)!" \
	  man/$${man} >$(man1dir)/$${man}; \
	  chmod 644 $(man1dir)/$${man}; \
	done)
	  
	$(mkinstalldirs) $(mandir)/man4
	(for man in $(sbutils_MAN4); do \
	  sed -e "s!%prefix%!$(prefix)!" \
	  -e "s!%libdir%!$(libdir)!" \
	  -e "s!%datadir%!$(datadir)!" \
	  -e "s!%sysconfdir%!$(sysconfdir)!" \
	  -e "s!%VERSION%!$(VERSION)!" \
	  man/$${man} >$(man4dir)/$${man}; \
	  chmod 644 $(man4dir)/$${man}; \
	done)
